var totalPage;
var c = require('checkAuthority');
var s = require('selectTransformat');
var m = require('model');
/*修改密码*/
function pageChange(page) {
	$.ajax({
		type: "post",
		url: "./user/finduser.bto",
		async: false,
		data: {
			page: page,
		},
		success: function(data) {
			s.transformatStatus('type');
			s.selectTransformat();
			var authorityCode = data.code;

			function template() {
				var myTemplate = __inline('main.handlebars');
				$('#table').html(myTemplate(data.other.data));
				totalPage = data.other.totalData;

				$('tbody td:last-of-type').each(function() {
					var delUserBtn = $(this).children('.delUser');
					var frozenUserBtn = $(this).children('.frozenUser');
					var editUserBtn = $(this).children('.editUser');

					delUserBtn.on('click', function() {
						var id = $(this).attr("data-id");
						m.model('删除用户', '#delUserModal');
						manageUser(id, 2);
					});

					frozenUserBtn.on('click', function() {
						var id = $(this).attr("data-id");
						m.model('冻结用户', '#frozenUserModal');
						manageUser(id, 1);
					});

					editUserBtn.on('click', function() {
						var id = $(this).attr("data-id");
						manageUser(id);
					});

				});
			}
			c.authority(authorityCode, template);
		}
	});
}

function manageUser(id, status) {
	if (status) {
		/*删除或冻结*/
		$('.btnSure').on('click', function() {
			editUser(id, status);

			$.ajax({
				type: 'post',
				url: './user/editusermanager.bto?id=' + id,
				data: {
					status: status
				},
				success: function(data) {
					window.location.href = "main.html"
				}
			});

		});
	} else {
		/*修改弹窗*/
		$.ajax({
			type: "post",
			url: "./user/editusermanager.bto?id=" + id,
			success: function(data) {
				var editUserTemplate = __inline('editUser.handlebars');
				$('#editUserModal').html(editUserTemplate(data.other.value));

				var passwd;
				$('.editPassword').blur(function() {
					passwd = $(this).val();
				})
				$('#confirmPasswd1').blur(function() {
					var confirmPasswd = $(this).val();
					if (confirmPasswd != passwd) {
						$('.error-tip').html('两次输入密码不一致！').show(200);
						$('#editUserModal button[type=submit]').attr('disabled', 'disabled');
					} else {
						$('.error-tip').hide(200);
						$('#editUserModal button[type=submit]').removeAttr('disabled');
					}
				});
				$('#editUserModal button[type=submit]').on('click', function() {
					var data = $('#editUserForm1').serialize();
					editUser(id, data);
				});
			}
		});
	}
}
pageChange(1);
$.jqPaginator('#pagination1', {
	totalCounts: totalPage,
	pageSize: 10,
	onPageChange: function(num, type) {
		pageChange(num);
	}
});

/*修改用户*/
function editUser(id, data) {
	$.ajax({
		type: 'post',
		url: './user/editusermanager.bto?id=' + id,
		data: data,
		success: function(data) {
			alert('修改成功!');
			window.location.href = "main.html";
		}
	});
}

/*添加用户*/
var addUserTemplate = __inline('addUser.handlebars');
$('#addUserModal').html(addUserTemplate);
$('.addUserPassword1').blur(function() {
	var confirmPasswd = $(this).val();
	var passwd = $('.addUserPassword').val();
	if (confirmPasswd != passwd) {
		$('.error-tip').html('两次输入密码不一致！').show(200);
	} else {
		$('.error-tip').hide(200);
	}
});
$('.account').blur(function() {
	if ($(this).val() != "") {
		$('#addUserModal button[type=submit]').removeAttr('disabled');
	} else {
		$('#addUserModal button[type=submit]').attr('disabled', 'disabled');
	}
});
$('#addUserForm button[type=submit]').on('click', function() {
	$.ajax({
		type: 'post',
		url: './user/adduser.bto',
		data: $('#addUserForm').serialize(),
		success: function(data) {
			window.location.href = "main.html";
		}
	});
});